package com.example.demo.base.util;

import org.springframework.util.Base64Utils;
import sun.misc.BASE64Decoder;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

public class CommonUtil {
    /**
     * 获取；两个日期中间的日期
     * @param start 开始日期
     * @param end 结束日期
     * @return List
     */
    public static List<String> getBetweenDate(String start,String end){
        LocalDate startDay = LocalDate.parse(start);
        LocalDate endDay = LocalDate.parse(end);
        long distance = ChronoUnit.DAYS.between(startDay, endDay);
        List<String> list = new ArrayList<>((int)distance);
        Stream.iterate(startDay, d -> {
            return d.plusDays(1);
        }).limit(distance + 1).forEach(f -> {
            list.add(f.toString());
        });
        return list;
    }

    public static boolean canParseInt(String str){
        if(str == null){
            return false;
        }
        return str.matches("\\d+");
    }

    //获取随机值
    public static int getRandomCount(int limit){
        return (int) (limit*Math.random());
    }

    public static String getTimeOld(Timestamp time){
        long addTime = time.getTime();
        long nowTime = Timestamp.valueOf(LocalDateTime.now()).getTime();
        String msg = "";


        long dateTemp1 = (nowTime-addTime) / 1000; // 秒
        long dateTemp2 = dateTemp1 / 60; // 分钟
        long dateTemp3 = dateTemp2 / 60; // 小时
        long dateTemp4 = dateTemp3 / 24; // 天数
        long dateTemp5 = dateTemp4 / 30; // 月数
        long dateTemp6 = dateTemp5 / 12; // 年数
        if (dateTemp6 > 0) {
            msg = dateTemp6 + "年前";
        } else if (dateTemp5 > 0) {
            msg = dateTemp5 + "个月前";
        } else if (dateTemp4 > 0) {
            msg = dateTemp4 + "天前";
        } else if (dateTemp3 > 0) {
            msg = dateTemp3 + "小时前";
        } else if (dateTemp2 > 0) {
            msg = dateTemp2 + "分钟前";
        } else if (dateTemp1 > 0) {
            msg = "刚刚";
        }
        return msg;
    }


    public static String getItemID( int n )
    {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        for ( int i = 0; i < n; i++ )
        {
            String str = random.nextInt( 2 ) % 2 == 0 ? "num" : "char";
            if ( "char".equalsIgnoreCase( str ) )
            { // 产生字母
                int nextInt = random.nextInt( 2 ) % 2 == 0 ? 65 : 97;
                // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
                val.append((char) (nextInt + random.nextInt(26)));
            }
            else { // 产生数字
                val.append(String.valueOf(random.nextInt(10)));
            }
        }
        return val.toString();
    }

    public static String filterSpecialCharAndUpper(String s){
        s = s.replaceAll("[^0-9a-zA-Z-]","");
        return s.toUpperCase();
    }

    public static List<byte[]> md5ToByte(String base64Img){
        List<byte[]> list = new ArrayList<>();
        if (base64Img.equals("")){
            return list;
        }
        String[] str = base64Img.split("##");
        BASE64Decoder decoder = new BASE64Decoder();
        for (String base64Str:str){
            //Base64解码
            byte[] b = new byte[0];
            try {
                b = decoder.decodeBuffer(base64Str);

            for(int i=0;i<b.length;++i) {
                if(b[i]<0) {
                    //调整异常数据
                    b[i]+=256;
                }
            }
            list.add(b);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }



}
